package com.manner.groupon.schedu;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.manner.groupon.mapper.StoreMapper;
import com.manner.groupon.mapper.UserMapper;
import com.manner.groupon.model.entity.Store;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Executors;

import static com.manner.groupon.constant.BusinessConstant.ONE_NUMBER;
import static com.manner.groupon.constant.RedisCacheKeyPrefix.APPLICATION_PROGRAM_EXPOSURE_LEVEL;
import static com.manner.groupon.constant.RedisCacheKeyPrefix.STORE_VISIT_NUMBER_OF_PEOPLE;

/**
 * @Author: Manner
 * @Description: 商家后台主页数据分析记录
 * @DateTime: 2024/12/9 15:24
 */
@Component
@RequiredArgsConstructor
public class StoreDataAssayRecord {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    private final StoreMapper storeMapper;


    /**
     * @description: 存储当天的每个店铺的访问人数
     * @author Manner
     * @date 2024/12/9 15:49
     */
    @Scheduled(cron = "55 59 23 * * ?")
    public void recordStoreVisitPeopleNumber() {
        LocalDate date = LocalDate.now();
        //所有店铺信息
        List<Store> stores = storeMapper.selectList(new LambdaQueryWrapper<Store>().ne(Store::getStatus, ONE_NUMBER));
        //存储每个店铺信息编号和访问人数的容器
        HashMap<String, String> storeToDayVisitPeopleNumber = new HashMap<>();
        //遍历存储
        stores.forEach(store -> {
            storeToDayVisitPeopleNumber.put(store.getStoreNo(), store.getNumberOfVisitors().toString());
        });
        //存入今天的数据
        stringRedisTemplate.opsForHash().putAll(STORE_VISIT_NUMBER_OF_PEOPLE + date, storeToDayVisitPeopleNumber);
    }

}
